package com.breezelin.card.app.recyclerview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.breezelin.card.app.R;
import com.breezelin.card.recyclerview.CardDisplayView;

import java.util.Collections;
import java.util.LinkedList;

public class CardRecyclerViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_recycler_view);

        final CardDisplayView cdv = (CardDisplayView) findViewById(R.id.card_display);
        final Integer[] origin = new Integer[]{
                Color.BLACK,
                Color.RED,
                Color.BLUE,
                Color.YELLOW,
                Color.CYAN,
                Color.GREEN,
                Color.MAGENTA,
        };

        final LinkedList<Integer> values = new LinkedList<>();
        Collections.addAll(values, origin);

        cdv.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                TextView itemView = new TextView(parent.getContext());
                itemView.setTextSize(54);
                itemView.setTextColor(Color.WHITE);
                itemView.setLayoutParams(new RecyclerView.LayoutParams(500, 500));
                itemView.setAlpha(0.8f);
                return new RecyclerView.ViewHolder(itemView) {
                };
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                TextView view = (TextView) holder.itemView;
                view.setText(String.valueOf(position + 1));
                view.setBackgroundColor(values.get(position));
            }

            @Override
            public int getItemCount() {
                return values.size();
            }
        });
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Collections.addAll(values, origin);
                cdv.getAdapter().notifyDataSetChanged();
            }
        });
        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cdv.scrollToPosition(0);
            }
        });
    }
}
